package com.example.demo.controller.surperController;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.AgriProductMapper;
import com.example.demo.Mapper.SurperMapper.AgriProductSurperMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.AgriProduct;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/agriProductSurper")
public class AgriProductSurperController {
    @Resource
    AgriProductSurperMapper agriProductSurperMapper;

    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(agriProductSurperMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        Page<AgriProduct> agriProductPage = agriProductSurperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(agriProductPage);
    }

    /**
     * 农产品编号查找
     *
     * @param id
     * @return
     */

    @GetMapping("/findById/{id}")
    public Result<?> findById(@PathVariable String id) {
        AgriProduct agriProduct = agriProductSurperMapper.selectById(id);
        List<AgriProduct> agriProductList = new ArrayList<>();
        if (agriProduct != null) {
            agriProductList.add(agriProduct);
            return Result.success(agriProductList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据农产品名称模糊查找
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public Result<?> findByComName(@PathVariable String name) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getName, name);
        List<AgriProduct> agriProductList = agriProductSurperMapper.selectList(wrapper);
        if (agriProductList.size() > 0)
            return Result.success(agriProductList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据类别模糊查找
     *
     * @param kind
     * @return
     */
    @GetMapping("/findByKind/{kind}")
    public Result<?> findByKind(@PathVariable String kind) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getKind, kind);
        List<AgriProduct> agriProductList = agriProductSurperMapper.selectList(wrapper);
        if (agriProductList.size() > 0)
            return Result.success(agriProductList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据存储地址模糊查找
     *
     * @param add
     * @return
     */
    @GetMapping("/findByAdd/{add}")
    public Result<?> findByAdd(@PathVariable String add) {
        LambdaQueryWrapper<AgriProduct> wrapper = Wrappers.<AgriProduct>lambdaQuery();
        wrapper.like(AgriProduct::getInventoryAdd, add);
        List<AgriProduct> agriProductList = agriProductSurperMapper.selectList(wrapper);
        if (agriProductList.size() > 0)
            return Result.success(agriProductList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delById/{id}")
    public Result<?> delete(@PathVariable String id) {
        int res = agriProductSurperMapper.deleteById(id);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息
     *
     * @param id
     * @param name
     * @param kind
     * @param handleDate
     * @param inventory
     * @param inventoryAdd
     * @param deadline
     * @param note
     * @return
     */

    @GetMapping(value = "/add")
    public Result<?> saveA(@RequestParam String id,
                           @RequestParam String name,
                           @RequestParam String kind,
                           @RequestParam String handleDate,
                           @RequestParam String inventory,
                           @RequestParam String inventoryAdd,
                           @RequestParam String deadline,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (id.isEmpty() || name.isEmpty() || kind.isEmpty() || handleDate.isEmpty() || inventory.isEmpty() || inventoryAdd.isEmpty() || deadline.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        AgriProduct agriProduct = new AgriProduct(Integer.valueOf(id), name, kind, handleDate, Double.valueOf(inventory), inventoryAdd, deadline, note, Integer.valueOf(tenantId));
        agriProductSurperMapper.insert(agriProduct);
        return Result.success();
    }

    /**
     * 根据农产品编号更新信息
     *
     * @param id
     * @param name
     * @param kind
     * @param handleDate
     * @param inventory
     * @param inventoryAdd
     * @param deadline
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String id,
                                @RequestParam String name,
                                @RequestParam String kind,
                                @RequestParam String handleDate,
                                @RequestParam String inventory,
                                @RequestParam String inventoryAdd,
                                @RequestParam String deadline,
                                @RequestParam String note,
                                @RequestParam String tenantId) {
        if (id.isEmpty() || name.isEmpty() || kind.isEmpty() || handleDate.isEmpty() || inventory.isEmpty() || inventoryAdd.isEmpty() || deadline.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        AgriProduct agriProduct = new AgriProduct(Integer.valueOf(id), name, kind, handleDate, Double.valueOf(inventory), inventoryAdd, deadline, note, Integer.valueOf(tenantId));
        agriProductSurperMapper.updateById(agriProduct);
        return Result.success();
    }
}
